﻿using System;
using PathFind.Algo;
using UnityEngine;
using UnityEngine.EventSystems;

namespace PathFind {
    public class InputDetector : MonoBehaviour {
        public SquireEdit edit;
        public SquireGrid grid;

        private void Update() {
            var leftClick = Input.GetMouseButton(0);
            var rightClick = Input.GetMouseButtonUp(1);
            if (!leftClick && !rightClick) {
                return;
            }
            if (EventSystem.current.IsPointerOverGameObject()) {
                return;
            }
            HandleInput(rightClick);
        }

        public Coordinate fromCell = new Coordinate(-1, -1);
        public Coordinate toCell = new Coordinate(-1, -1);

        private void HandleInput(bool rightClick) {
            System.Diagnostics.Debug.Assert(Camera.main != null, "Camera.main != null");
            Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.DrawLine(inputRay.origin, inputRay.origin + inputRay.direction * 300);
            if (Physics.Raycast(inputRay, out RaycastHit hit)) {
                if (rightClick) {
                    HandleRightClick(hit);
                    return;
                }

                HandleLeftClick(hit);
            }
        }

        private void HandleLeftClick(RaycastHit hit) {
            if (edit.clickAction == ClickAction.ChangeStartEnd) {
                ChangeStartEndCell(hit.point);
            }
            else if (edit.clickAction == ClickAction.ChangeSetUnwalk) {
                ChangeCellWalkable(hit.point, false);
            }
            else if (edit.clickAction == ClickAction.ChangeSetWalk) {
                ChangeCellWalkable(hit.point, true);
            }
        }

        private void HandleRightClick(RaycastHit hit) {
            var co = grid.GetCoordinate(hit.point);
            Debug.Log($"Right click on ({co.x},{co.y}))");
            // grid.ChangeColor(co, Color.yellow);
        }

        private void ChangeCellWalkable(Vector3 hitPoint, bool canWalk) {
            var clickCoord = grid.GetCoordinate(hitPoint);
            if (fromCell == clickCoord || toCell == clickCoord) {
                return;
            }
            edit.SetPassable(clickCoord, canWalk);
        }

        private void ChangeStartEndCell(Vector3 hitPoint) {
            var clickCoord = grid.GetCoordinate(hitPoint);
            if (!edit.InBounds(clickCoord)) {
                Debug.Log("起点或终点越界");
                return;
            }
            if (!edit.IsPassable(clickCoord)) {
                Debug.Log("起点和终点不能是不可走点");
                return;
            }
            if (fromCell == clickCoord) {
                return;
            }
            if (toCell == clickCoord) {
                return;
            }
            if (Input.GetKey(KeyCode.LeftShift)) {  // 设置目标点
                if (edit.InBounds(toCell)) {
                    edit.ResetColor(toCell);
                }
                toCell = clickCoord;
                edit.SetEndPoint(toCell);
            }
            else {
                if (edit.InBounds(fromCell)) {
                    edit.ResetColor(fromCell);
                }
                fromCell = clickCoord;
                edit.SetStartPoint(fromCell);
            }
        }
    }
}